﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Data.SqlClient;
using UserAuthority;

namespace MainProgram
{
    public partial class FormLogin : Form
    {

        SqlConnection myConnection = new SqlConnection();
        SqlCommand myCommand = new SqlCommand();
        FormMain myParentForm;

        public FormLogin()
        {
            InitializeComponent();
        }

        public FormLogin(FormMain parentForm)
        {
            myParentForm = parentForm;
            InitializeComponent();
        }

        private void buttonX1_Click(object sender, EventArgs e)
        {

            #region Use ADO.NET method
            //try
            //{
            //    SqlConnection userConnection = new SqlConnection(@"server=122.205.7.226\SQLEXPRESS;database=UserData;uid=root;pwd=123");
            //    userConnection.Open();

            //    SqlCommand userCommand = new SqlCommand();
            //    userCommand.Connection = userConnection;
            //    userCommand.CommandText = @"select * from UserInfo where Name='" + textBox1.Text + @"'";
            //    SqlDataReader userReader = userCommand.ExecuteReader();
            //    userReader.Read();

            //    string pwd = userReader["Password"].ToString().Trim();
            //    if (pwd == textBox2.Text)
            //    {
            //        MessageBox.Show("登录成功");
            //        string authority = userReader["Authority"].ToString();

            //        myParentForm.LoginState = true;
            //        myParentForm.SetUser(textBox1.Text, Convert.ToInt32(authority));

            //        this.Close();
            //    }
            //    else
            //    {
            //        MessageBox.Show("密码错误");
            //    }
            //}
            //catch (Exception ex)
            //{
            //    MessageBox.Show(ex.Message);
            //}
            #endregion


            #region Use Linq method
            try
            {
                UserDataDataContext uddc = new UserDataDataContext();
                var results = from c in uddc.UserInfo
                              where c.Name == textBox1.Text
                              where c.Password == textBox2.Text
                              select c;

                if (results.Count() != 0)
                {
                    foreach (var item in results)
                    {

                        MessageBox.Show("登录成功");

                        myParentForm.LoginState = true;
                        myParentForm.SetUser(item.Name, item.Authority);

                        this.Close();

                    }
                }
                else
                {
                    MessageBox.Show("登录失败");
                }
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            #endregion

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            textBox2.Text = "";
        }

        private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (Convert.ToInt32(e.KeyChar)==13)
            {
                this.buttonX1_Click(sender,e);
            }
        }
    }
}
